package com.mikepenz.app.activity;

import android.app.ProgressDialog;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.MarkerOptions;
import com.mikepenz.app.R;
import com.mikepenz.app.adapter.ListViewAdapter;
import com.mikepenz.bean.ParkBean;
import com.mikepenz.presenter.ParkPresenter;
import com.mikepenz.presenter.imp.ParkPresenterImp;
import com.mikepenz.utils.CookicUntil;
import com.mikepenz.view.ParkView;
import com.quickdv.activity.MapActivity;
import com.quickdv.activity.view.CustomListView;
import com.quickdv.until.AlertHelper;
import com.quickdv.until.ViewHolder;
import com.squareup.picasso.Picasso;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import butterknife.InjectView;

/**
 * 当前停车场的基本信息
 * *
 */
public class ParkActivity extends MapActivity implements ParkView {
    List<Map<String, Object>> data;

    LatLng[] la = new LatLng[]{new LatLng(34.7466, 113.625367)};
    ProgressDialog dialog;

    ParkPresenter presenter;
    @InjectView(R.id.park_address)
    TextView parkAddress;
    @InjectView(R.id.park_phone)
    ImageView parkPhone;
    @InjectView(R.id.park_rest_car)
    CustomListView parkRestCar;
    @InjectView(R.id.myscrool)
    ScrollView myscrool;

    ListViewAdapter adapter;

    @Override
    public int getLayoutID() {
        return R.layout.activity_park;
    }

    @Override
    public void initView() {

        // Handle Toolbar
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        adapter = new ListViewAdapter(this, data, R.layout.activity_park_item, new String[]{"carname", "carrying","course", "cartype"}, new int[]{R.id.park_car_name, R.id.park_car_carry_value, R.id.park_car_mileage_value, R.id.park_car_type_value}){
            @Override
            protected void renderData(int position, View convertView) {

                super.renderData(position, convertView);
                ImageView view = ViewHolder.get(convertView, R.id.park_car_pic);
                if (data.get(position).get("pic") != null) {
                    String obj = (String) data.get(position).get("pic");
                    Picasso.with(c).load(CookicUntil.IMG_URL + obj).centerCrop().resize(150, 120).error(R.drawable.loadfailed).placeholder(R.drawable.loading).into(view);
                }


                TextView price = ViewHolder.get(convertView,R.id.park_car_price_value);
                float time =Math.round((Float.valueOf((String)data.get(position).get("basictariff")))  * 100);
                price.setText( time/100.0 + "元/分钟");
            }
        };
        dialog = new AlertHelper(this).LoadingAlert();
        mapView = findView(R.id.map);
        parkRestCar.setAdapter(adapter);
        parkRestCar.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
               presenter.onItemClick(i);
            }
        });
    }

    @Override
    protected void init() {
        super.init();
        ParkBean parkBean = (ParkBean) getIntent().getExtras().getSerializable("park");

        presenter = new ParkPresenterImp(this);
        presenter.initParkInfo(parkBean);
        presenter.getRestCar(parkBean.getParkId());
    }

    @Override
    public void initActionbar() {
        getSupportActionBar().setTitle("停车场");
    }

    @Override
    public void initData() {
        data =new ArrayList<>();
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        ScrollView scroll = findView(R.id.myscrool);
        scroll.scrollTo(0, 0);
    }

    @Override
    public void showError(String error) {
        Toast.makeText(this,error,Toast.LENGTH_LONG).show();
    }

    @Override
    public void showLoading() {
        dialog.show();
    }

    @Override
    public void dimissLoad() {
        dialog.dismiss();
    }

    @Override
    public void carList(List<Map<String, Object>> car,boolean isRefresh) {
        if (isRefresh) data.clear();
        data.addAll(car);
    }

    @Override
    public void notifyDateChange() {
        adapter.notifyDataSetChanged();
    }


    @Override
    public void drawPostions(LatLng latLng) {
        aMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(
                new LatLng(latLng.latitude, latLng.longitude), 14, 0, 30)), 1000, null);

        MarkerOptions options = new MarkerOptions().anchor(0.5f, 0.5f).icon(BitmapDescriptorFactory.fromResource(R.drawable.location_icon))
                .position(new LatLng(latLng.latitude, latLng.longitude))
                .draggable(true).period(10);
        aMap.addMarker(options);
    }

    @Override
    public void setAddress(String address, LatLng latLng) {
        parkAddress.setText(address);
    }

    @Override
    public void setCallPhone(View.OnClickListener listener) {
        parkPhone.setOnClickListener(listener);
    }
}
